<template>
    <div class="workDay">
        <p class="date_change">
            <el-icon @click="previousMonth()"><ArrowLeftBold /></el-icon>
            <span>{{ moment(month).format('YYYY年MM月') }}</span>
            <el-icon @click="nextMonth()"><ArrowRightBold /></el-icon>
            <span class="today" @click="comeToday()">返回今天</span>
        </p>
        <ul ref="uploadImgBtn">
            <li class="grey">SUN.日</li>
            <li>MON.一</li>
            <li>TUES.二</li>
            <li>WEB.三</li>
            <li>THUR.四</li>
            <li>FRI.五</li>
            <li class="grey">SAT.六</li>
        </ul>
        <el-calendar v-model="value" >
            <template #date-cell="{ data }">
                <div @click="changeStatus(item)"  :style="{ height: item.date.substr(0, 10) == data.day ? '100%' : '' }" v-for="(item, index) in worklist" :key="index">
                    <div style="height: 100%;" v-if="item.date.substr(0, 10) == data.day">
                        <p class="seletcss" :class="data.isSelected ? 'is-selected' : ''">{{ data.day.substr(data.day.length - 2, 2) }}</p>
                        <div class="isPunched" v-if="item.type == 1"></div>
                        <div class="noPunched" v-if="item.type == 2"></div>
                    </div>
                </div>
            </template>
        </el-calendar>
         <el-dialog class="deleteDialog" v-model="deleteDialog" title="" top="30vh" width="20.5%">
            <img width="50" height="50" src="@/assets/images/mainImg/gth.png" />
            <p class="blk">{{ dialogText }}</p>
            <template #footer>
                <span class="dialog-footer" style="text-align: center;">
                    <el-button @click="deleteDialog = false">取消</el-button>
                    <el-button type="primary" @click="submitHoliday">确定</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import { getHolidayList, updHoliday } from '@/api/workDay';
import moment from 'moment';
import { ElMessage } from 'element-plus';
const value = ref(new Date());
let month = ref(moment().locale('zh-cn').format('YYYY-MM'));
const { proxy } = getCurrentInstance();
const data = reactive({
    worklist: [],
    selWork:{}
});
const { worklist , selWork } = toRefs(data);
const dialogText = ref(null);
const deleteDialog = ref(false);
let prevBtn = undefined;
let nextBtn = undefined;
let todayBtn = undefined;
const getWorkDayList = () => {
    let param = {
        month:month.value
    }
    getHolidayList(param).then(res => {
        if (res.code === 200) {
            worklist.value = res.data; 
            if (res.data.length == 0) {
                ElMessage({ message: '该月暂未考核', type: 'error', duration: 1 * 1000 })
            }
        }
    })
}
const initDom = () =>{
    document.getElementsByTagName('thead')[0].style.visibility = 'hidden';
    document.getElementsByTagName('thead')[0].style.display = 'block';
    document.getElementsByTagName('thead')[0].style.height = '0';
    document.getElementsByClassName('el-calendar__header')[0].style.visibility = 'hidden';
    document.getElementsByClassName('el-calendar__header')[0].style.height = '0';
    prevBtn = document.querySelector('.el-calendar__button-group .el-button-group>button:nth-child(1)');
    nextBtn = document.querySelector('.el-calendar__button-group .el-button-group>button:last-child');
    todayBtn = document.querySelector('.el-calendar__button-group .el-button-group>button:nth-child(2)');
}
const submitHoliday = () => {
    if (selWork.value.id) {
       let data = {
            date: selWork.value.date,
            id: selWork.value.id,
            type: selWork.value.type == 1 ? 2 : 1
        }
        updHoliday(data).then(res => {
            if (res.code === 200) {
                ElMessage({ message: '更改成功', type: 'success', duration: 1 * 1000 });
                deleteDialog.value = false;
                getWorkDayList();
            }
         })
    }
    
}
const changeStatus = (item) => {
    let nowDate = moment().locale('zh-cn').format('YYYY-MM-DD');
    if (moment(item.date).isBefore(nowDate)) {
        ElMessage({ message: '无法修改过去日期', type: 'error', duration: 1 * 1000 })
        return;
    }
    let text = item.type == 1 ? '休息日?' : '工作日?';
    dialogText.value = '更改' + moment(item.date).format('YYYY-MM-DD') + '为' + text;
    selWork.value = item;
    deleteDialog.value = true;
}
const previousMonth = () => {
    month.value = moment(month.value).subtract(1, 'months').format('YYYY-MM');
    prevBtn.click();
    getWorkDayList();
}
const nextMonth = () => {
    month.value = moment(month.value).add(1, 'months').format('YYYY-MM');
    nextBtn.click();
    getWorkDayList();
}
const comeToday = () => {
    month.value = moment().locale('zh-cn').format('YYYY-MM');
    todayBtn.click();
    getWorkDayList();
}
onMounted(() => {
    initDom();
});
getWorkDayList();
</script>
<style lang="scss" scoped>
    .workDay{
        height: 100%;
        background-color: #ffffff;
        border-radius: 10px;
        padding: 5px;
        overflow: auto;
        ul {
            padding: 20px 20px 0px 20px;
            display: flex;
            justify-content: space-evenly;
            text-align: center;
            margin: 0;
            min-width: 1116px;
            li {
                list-style: none;
                width: 13.5%;
                background: #ffdbdd;
                color: #be1a21;
                padding: 8px 0;
                font-weight: 600;
                border-radius: 3px;
            }
        }
        .date_change {
            padding-top: 30px;
            position: relative;
            margin: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            span {
                margin: 0 20px;
                font-size: 22px;
            }
            .el-icon {
                cursor: pointer;
            }
        }
        .today {
            position: absolute;
            right: 20px;
            padding: 6px 10px;
            background: #eeeeee;
            border-radius: 4px;
            color: #999999;
            cursor: pointer;
            font-size: 16px !important;
        }
        .today:hover {
            background: #d0d0d0;
            color: #ffffff;
        }

        .isPunched {
            display: inline-block;
            height: 100%;
            width: 100%;
            background: #FFC4C9;
        }
        .noPunched {
            display: inline-block;
            // border-radius: 50%;
            height: 100%;
            width: 100%;
            background: #dddddd;
        }
        .is-selected {
            color: #1989fa !important;
        }
        :deep(.el-calendar-table)  {
            .el-calendar-day {
                position: relative;
                padding: 0;
            }
        }
        .seletcss {
            z-index: 1;
            position: absolute;
            font-size: 24px;
            top: 17%;
            width: 100%;
            left: 0;
            text-align: center;
            color: #ffffff;
        }
        :deep(.deleteDialog) {
            .el-dialog__body {
                text-align: center;
            }
            .blk {
                font-weight: 600;
                color: #000;
            }
            .el-dialog__footer{
                text-align: center;
            }
        }
        :deep(.el-calendar){
             .el-calendar-day {
                height: 123px;
                min-width: 148px;
                max-width: 246px;
            }
            .el-calendar__body {
                padding-bottom: 0;
                padding: 12px 30px 25px;
            }
            .current {
                position: relative;
            }
        }
        :deep(.el-calendar-day):hover{
            color:red !important;
            background-color:#ffdbdd !important;
        }
    }
</style>